package com.rarebot.script.randoms;

import java.util.HashMap;
import java.util.Map;

import com.rarebot.script.Random;
import com.rarebot.script.ScriptManifest;
import com.rarebot.script.wrappers.RSComponent;

/**
 * Closes interfaces that scripts may open by mistake.
 */
@ScriptManifest(authors = {"Anonymau5", "WhoKnew", "HeyyamaN", "Pervy Shuya"}, name = "InterfaceCloser", version = 2.0)
public class CloseAllInterface extends Random {
	private final Map<Integer, Integer> components = new HashMap<Integer, Integer>();

	{
		components.put(14, 11); // Pin settings -GOOD
		components.put(17, 13); // Death items -GOOD
		components.put(18, 37); // "Get Ready to Respawn" Confirm button. -SCRIPTWRITERS SHOULD WRITE THEIR OWN, BUT ILL LEAVE IT
		//components.put(109, 14); // Grand exchange collection -GOOD
		components.put(149, 226); //Level 10 membership solicitation -QUESTIONABLE EXISTENCE
		components.put(157, 13); // Quick chat help -GOOD
		components.put(206, 13); // Price check -GOOD
		components.put(266, 0); // Tombstone - FIXED
		components.put(382, 14); //Wilderness check -GOOD 
		components.put(266, 11); // Grove - QUESTIONABLE EXISTENCE
		//components.put(275, 8); // Quest -CORRECT ONE NOW IS (interface.get(1245).getComponent(314).getComponent(14))
		components.put(276, 76); // Soul Wars Rewards - NEEDS TESTING
		components.put(301, 31); //Req. Assist Screen  
		components.put(1218, 73); // Stats - FIXED
		components.put(594, 88); // Report Abuse -FIXED
		components.put(667, 87); // Equipment Bonus - FIXED
		components.put(732, 208); // Fist of Guthx Reward Shop - NEEDS TESTING
		components.put(742, 18); // Graphic -GOOD
	//	components.put(429, 18); // Audio - broken
		components.put(755, 44); // World Map -GOOD
		components.put(764, 18); // Objectives -QUESTIONABLE EXISTENCE
		components.put(767, 10); // Bank of RuneScape - Help -GOOD
		components.put(895, 19); // Advisor -QUESTIONABLE EXISTENCE
		components.put(917, 112); // Task List -FIXED
		components.put(1011, 51); // Pest Control Rewards ( Commendation Rewards ) -NEEDS TESTING
		components.put(1083, 181); // Livid Farm Rewards - NEEDS TESTING
		components.put(1107, 157); // Clan Vexillum -FIXED
		components.put(1127, 16); //Hybrid Gloves selection ( Fist of Guthix ) -NEEDS TESTING
		components.put(1252, 5); //Wheel of bullshit "X" button on popup thing
		components.put(1253, 108); //Wheel of bullshit "hide" button inside of game
	}

	@Override
	public boolean activateCondition() {
		RSComponent quest = interfaces.get(1245).getComponent(314).getComponent(14);
		if (game.isLoggedIn()) {
			if (quest != null && quest.isValid()) {
				return true;
			}
			for (Map.Entry<Integer, Integer> c : components.entrySet()) {
				if (interfaces.getComponent(c.getKey(), c.getValue()).isValid()) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public int loop() {
		RSComponent quest = interfaces.get(1245).getComponent(314).getComponent(14);
		sleep(random(500, 900));
		if (quest != null && quest.isValid()) {
			if (quest.doClick()) {
				sleep (900, 1100);
			}
		}
		for (final Map.Entry<Integer, Integer> c : components.entrySet()) {
			if (interfaces.getComponent(c.getKey(), c.getValue()).isValid()) {
				if (interfaces.getComponent(c.getKey(), c.getValue()).doClick()) {
					sleep(random(900, 1100));
					if (random(0, 4) == 0) {
						mouse.moveSlightly();
					}
					break;
				}
			}
		}
		return -1;
	}
}